package com.cskaoyan.user.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.commons.util.CookieUtil;
import com.cskaoyan.user.constants.UserRetCode;
import com.cskaoyan.user.dto.*;
import com.cskaoyan.user.service.IKaptchaService;
import com.cskaoyan.user.service.IUserLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @Description
 * @Author 徐成喜
 * @Data 2022/7/29 15:54
 */
@RestController
public class UserLoginController {

    @Autowired
    IUserLoginService loginService;

    @Autowired
    IKaptchaService kaptchaService;


    /**
     * @return
     * @description 用户登录
    */
    @PostMapping("user/login")
    public ResponseData userLoginNoToken(@RequestBody Map<String,String> map, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        //验证码
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        kaptchaCodeRequest.setCode(map.get("captcha"));
        String kaptcha_uuid = CookieUtil.getCookieValue(httpServletRequest, "kaptcha_uuid");
        kaptchaCodeRequest.setUuid(kaptcha_uuid);
        KaptchaCodeResponse kaptchaCodeResponse = kaptchaService.validateKaptchaCode(kaptchaCodeRequest);
        if (!kaptchaCodeResponse.getCode().equals(UserRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setErrorMsg(kaptchaCodeResponse.getMsg());
        }

        //用户名密码验证
        UserLoginRequest request = new UserLoginRequest();
        request.setUserName(map.get("userName"));
        request.setPassword(map.get("userPwd"));
        UserLoginResponse response = loginService.userLoginNoToken(request);

        if (UserRetCode.SUCCESS.getCode().equals(response.getCode())) {
            Cookie cookie = new Cookie("access_token",response.getToken());
            cookie.setPath("/");
            cookie.setMaxAge(60*60*24);
            httpServletResponse.addCookie(cookie);
            return new ResponseUtil().setData(response);
        }

        return new ResponseUtil().setErrorMsg(response.getMsg());

    }


    /**
     * @description 验证用户登录
     * @return
     */
    @GetMapping("user/login")
    public ResponseData userLoginByToken(HttpServletRequest request) {
        String user_info = request.getHeader("user_info");
        JSONObject jsonObject = JSON.parseObject(user_info);
        UserLoginGetTokenResponse tokenResponse = new UserLoginGetTokenResponse();
        if(jsonObject.get("file")!=null){
            tokenResponse.setFile(jsonObject.get("file").toString());
        }else {
            tokenResponse.setFile(null);
        }
        tokenResponse.setUid(jsonObject.get("uid").toString());
        return new ResponseUtil().setData(tokenResponse);
    }


    /**
     * @description 退出登录
     * @return
     */
    @GetMapping("/user/loginOut")
    public ResponseData userLoginOut(HttpServletResponse response) {
        Cookie cookie = new Cookie("access_token","");
        cookie.setPath("/");
        cookie.setMaxAge(0);
        response.addCookie(cookie);
        return new ResponseUtil().setData(null);
    }

    @PostMapping("/user/imageUpload")
    public ResponseData imageUpload(MultipartFile file){
        file.getContentType();

        return null;
    }


}
